package at.tuwien.seminar.scrum1.view.customer.list;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import at.tuwien.seminar.scrum1.model.Bill;
import at.tuwien.seminar.scrum1.model.Room;
import at.tuwien.seminar.scrum1.provider.AjaxDataProvider;
import at.tuwien.seminar.scrum1.provider.IListLoading;
import at.tuwien.seminar.scrum1.proxies.ReservationProxy;
import at.tuwien.seminar.scrum1.view.bill.list.BillList;
import at.tuwien.seminar.scrum1.view.reservation.list.ReservationList;
import at.tuwien.seminar.scrum1.view.room.list.RoomList;
import at.tuwien.seminar.scrum1.view.wicket.customer.CustomerDeleteActionPanel;
import at.tuwien.seminar.scrum1.view.wicket.customer.CustomerUpdateActionPanel;
import at.tuwien.seminar.scrum1.view.wicket.customer.ReservationActionPanel;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteTextField;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import at.tuwien.seminar.scrum1.configuration.Configuration;
import at.tuwien.seminar.scrum1.model.Customer;
import at.tuwien.seminar.scrum1.provider.DataProvider;
import at.tuwien.seminar.scrum1.proxies.CustomerProxy;
import at.tuwien.seminar.scrum1.view.titlepage.TitlePage;
import at.tuwien.seminar.scrum1.view.customer.create.CreateCustomer;
import org.apache.wicket.model.PropertyModel;

public class CustomerList extends WebPage implements IListLoading<Customer>{


    private List<Customer> list;
    private DefaultDataTable table;

    private String search;

	public CustomerList() {

        list = CustomerProxy.getInstance().getAll();

        add(new Link<Room>("roomLink") {

			@Override
			public void onClick() {
				setResponsePage(new RoomList());
			}

		});
		add(new Link<Customer>("customerLink") {

			@Override
			public void onClick() {
				setResponsePage(new CustomerList());
			}

		});

        add(new Link<Customer>("reservationLink") {

			@Override
			public void onClick() {
				setResponsePage(new ReservationList(ReservationProxy.getInstance().getAll()));
			}

		});

         add(new Link<Bill>("billLink") {

			@Override
			public void onClick() {
				setResponsePage(new BillList());
			}

		});

        Form<?> form = new Form("form");

		final SortableDataProvider<Customer> dataProvider = new AjaxDataProvider<Customer> (
				"lastName", this);

		List<IColumn<Customer>> columns = new ArrayList<IColumn<Customer>>();

        columns.add(new AbstractColumn<Customer>(new Model<String>("Actions"))
        {
            public void populateItem(Item<ICellPopulator<Customer>> cellItem, String componentId,
                IModel<Customer> model)
            {
                cellItem.add(new CustomerUpdateActionPanel(componentId, model));
            }
        });
		columns.add(new PropertyColumn<Customer>(new Model<String>("Vorname"), "firstName",
				"firstName"));
		columns.add(new PropertyColumn<Customer>(new Model<String>("Nachname"), "lastName",
				"lastName"));
		columns.add(new PropertyColumn<Customer>(new Model<String>("Tel"), "phone",
				"phone"));
		columns.add(new PropertyColumn<Customer>(new Model<String>("Email"), "email",
				"email"));


         columns.add(new AbstractColumn<Customer>(new Model<String>("Actions"))
		        {
		            public void populateItem(Item<ICellPopulator<Customer>> cellItem, String componentId,
		                IModel<Customer> model)
		            {
		                cellItem.add(new ReservationActionPanel(componentId, model));
		            }
		        });
        columns.add(new AbstractColumn<Customer>(new Model<String>("Actions"))
        {
            public void populateItem(Item<ICellPopulator<Customer>> cellItem, String componentId,
                IModel<Customer> model)
            {
                cellItem.add(new CustomerDeleteActionPanel(componentId, model));
            }
        });

		add(table = new DefaultDataTable<Customer>("datatable", columns, dataProvider,
                Configuration.ROWSPERPAGE));

        table.setOutputMarkupId(true);

        form.add(new AutoCompleteTextField<String>("search", new PropertyModel<String>(this, "search")) {
            @Override
            protected Iterator<String> getChoices(String input) {
                List<String> customers = new ArrayList<String>();

                for (Customer customer : CustomerProxy.getInstance().getAll()) {

                    String searchString = customer.getFirstName() + " " + customer.getLastName() + " [" + customer.getId() + "]";

                    if (searchString.contains(input)) {
                        customers.add(searchString);
                    }
                }

                return customers.iterator();
            }

        }.add(new AjaxFormComponentUpdatingBehavior("onkeypress") {

            @Override
            protected void onUpdate(AjaxRequestTarget ajaxRequestTarget) {
                if (search == null) {
                    search = "";
                }

                if (search != null) {
                    list.clear();

                    for (Customer customer : CustomerProxy.getInstance().getAll()) {
                        String searchString = customer.getFirstName() + " " + customer.getLastName() + " [" + customer.getId() + "]";

                    if (searchString.contains(search)) {
                            list.add(customer);
                        }
                    }

                    ajaxRequestTarget.addComponent(table);
                }
            }
        }).add(new AjaxFormComponentUpdatingBehavior("onchange") {

            @Override
            protected void onUpdate(AjaxRequestTarget ajaxRequestTarget) {
                if (search == null) {
                    search = "";
                }

                if (search != null) {
                    list.clear();

                    for (Customer customer : CustomerProxy.getInstance().getAll()) {
                        String searchString = customer.getFirstName() + " " + customer.getLastName() + " [" + customer.getId() + "]";

                    if (searchString.contains(search)) {
                            list.add(customer);
                        }
                    }

                    ajaxRequestTarget.addComponent(table);
                }
            }
        }));


        add(new Link<Customer>("back") {

            @Override
            public void onClick() {
                setResponsePage(new TitlePage());

            }

        });

		form.add(new Link<Customer>("newCustomerLink") {

            @Override
            public void onClick() {
                setResponsePage(new CreateCustomer());
            }

        });

        add(form);
	}

    public List<Customer> getList() {
        return list;
    }

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }
}
